package coolq.yaozhenyong.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.forte.qqrobot.anno.depend.AllBeans;
import com.forte.qqrobot.beans.messages.result.GroupList;
import com.forte.qqrobot.beans.messages.result.inner.Group;
import com.forte.qqrobot.component.forhttpapi.HttpApp;
import com.forte.qqrobot.component.forhttpapi.HttpConfiguration;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.QqRobotSet;
import coolq.yaozhenyong.custom.CocCustom;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimeUtils;
import coolq.yaozhenyong.custom.UpdateGroupList;
import coolq.yaozhenyong.service.QqRobotSetService;
@AllBeans
public class MyqqRobot implements HttpApp{
	public static String clashofclansforecaster[] = {"",TimeUtils.getStringDate(),"1"};
	public static CQCodeUtil cqCodeUtil;
	@Override
	public void before(HttpConfiguration link) {
		//直接在数据库取出配置项目
				QqRobotSetService bean = SpringContextUtil.getBean(QqRobotSetService.class);
				QqRobotSet entity = bean.getById(1);
				if(entity!=null) {
					if(entity.getLocalqqnick()!=null&&entity.getLocalqqnick().length()>1) {
						link.setLocalQQNick(entity.getLocalqqnick());
						systemout("Nick",entity.getLocalqqnick());
					}
					if(entity.getLocalqqcode()!=null&&entity.getLocalqqcode().length()>1) {
						link.setLocalQQCode(entity.getLocalqqcode());
						systemout("Code",entity.getLocalqqcode());
					}
					if(entity.getCqPath()!=null&&entity.getCqPath().length()>1) {
						link.setCqPath(entity.getCqPath());
						systemout("CqPath",entity.getCqPath());
					}
					if(entity.getEncode()!=null&&entity.getEncode().length()>1) {
						link.setEncode(entity.getEncode());
						systemout("Encode",entity.getEncode());
					}
					if(entity.getLinkIp()!=null&&entity.getLinkIp().length()>1) {
						link.setIp(entity.getLinkIp());
						systemout("LinkIp",entity.getLinkIp());
					}
					
					if(entity.getBacklog()!=null&&entity.getBacklog()>0) {
						link.setBacklog(entity.getBacklog());
						systemout("设置重连次数Backlog",entity.getBacklog()+"");
					}
					
					if(entity.getServerPath()!=null&&entity.getServerPath().length()>1) {
						link.setServerPath(entity.getServerPath());
						systemout("配置java服务器的请求路径ServerPath",entity.getServerPath());
					}
					
					if(entity.getServerPort()!=null&&entity.getServerPort()>0) {
						link.setServerPort(entity.getServerPort());
						systemout("配置酷Q端的端口地址ServerPort",entity.getServerPort()+"");
					}
					
					if(entity.getJavaPort()!=null&&entity.getJavaPort()>0) {
						link.setJavaPort(entity.getServerPort());
						systemout("配置java服务器的监听端口JavaPort",entity.getJavaPort()+"");
					}
					//设置重连次数
					//link.setBacklog(6);
					//configuration.setJavaPort(15514);
					//link.setServerPath(null);
					/*link.setJavaPort();//配置java服务器的监听端口，默认为15514*/	
					//.setServerPort(9999);//配置酷Q端的端口地址，默认为8877
			}
	}

	@Override
	public void after(CQCodeUtil cqCodeUtil, MsgSender sender) {
		String stringDate = TimeUtils.getStringDate();
		GroupList groupList = sender.GETTER.getGroupList();
		Group[] list = groupList.getList();
		String groups = "";
		for (Group group : list) {
			groups = groups + group.getCode() +group.getName()+"\r\n";
		}
		sender.SENDER.sendPrivateMsg("936642284", stringDate+"机器人启动成功!"
				+ "\r\n" +groups
				+ "￥￥开始管控￥￥");
		UpdateGroupList.uodate(sender, cqCodeUtil);
		MyqqRobot.cqCodeUtil = cqCodeUtil;
		String goyuqing = null;
		while (goyuqing==null) {
			System.out.println(TimeUtils.getStringDate()+" 获取鱼情！");
			goyuqing =  CocCustom.goyuqing();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
			}
		}
		if(goyuqing!=null){
			clashofclansforecaster[0] = goyuqing;
			clashofclansforecaster[1] = TimeUtils.getStringDate();
			clashofclansforecaster[2] = "1";
			sender.SENDER.sendPrivateMsg("936642284", "鱼请获取成功：\n"+goyuqing);
		}
	}
	public void systemout(String code,String value) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		System.out.println(df.format(new Date())+" QQRobot  配置属性[ "+code+" ]配置内容[ "+value+" ]");
	}
}
